// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        prefabLeven: cc.Prefab,
        objPanel:cc.Node,

        labelCurLevel:cc.Label,
        //关卡记录
        _curLevel:1,
        //设置prefab
        prefabSetting:cc.Prefab,
        //体力值
        labelTiLi:cc.Label,
        //提示次数
        labelTiSi:cc.Label,
    },



     onLoad () {
        cc.director.preloadScene("gameMain");
        this.init();
     },

     init(){
        this._curLevel=window.gameConfig.gameData.level;
        let levelNumber=(6-(this._curLevel%6)+1)
        levelNumber=new Number(this._curLevel)+levelNumber;

        this.labelTiSi.string = window.gameConfig.gameData.hintNumber;
        this.labelTiLi.string = window.gameConfig.gameData.coin;
        this.labelCurLevel.string= "第"+this._curLevel+"关";//关卡
     },

    start() {

    },
    onJsonBtnClick(event) {
        let btnName = event.target.name;
        window.audioMgr.playGameAudio("buttonCheck")
        switch (btnName) {
            case "btn_xg":
                cc.log("加载选关界面");
                event.interactable = false;
                this.loadLevenToScene();
            break;

            case "btn_startGame":
                cc.log("开始游戏");
                event.interactable = false;
                this.gameStart();
            break;

            case "btn_rank":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("开启排行榜");
                event.interactable = false;
                this.openRankLayer();
            break;  

            case "btn_getTL":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("获取体力");
                event.interactable = false;
            break;  
            
            case "btn_yq":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("邀请好友");
                event.interactable = false;
            break;  

            case "btn_setting":
                cc.log("打开设置");
                event.interactable = false;
                this.openSettingLayer();
            break;  

            case "btn_back":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("点击返回");
                event.interactable = false;
            break;  

            case "btn_addTL":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("添加体力");
                event.interactable = false;
            break;  

            case "btn_addTS":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("添加提示");
                event.interactable = false;
            break; 

            case "btn_fankui":
                window.onLoadPfPopTitle(this, "功能暂未开放,敬请期待!")
                cc.log("用户反馈");
                event.interactable = false;
            break;  

            default:
                cc.log("无对应按钮");
                break;
        }
    },
    /**
     * 打开设置界面
     */
    openSettingLayer(){
       var objSetting=cc.instantiate(this.prefabSetting);
       this.node.addChild(objSetting);
    },

    /**
     * 创建关卡
     */
    loadLevenToScene() {
        var objLeven = cc.instantiate(this.prefabLeven);
        objLeven.name="objLevel";
        this.node.addChild(objLeven);
        var objLevenCom = objLeven.getComponent("LevelScene");
        objLevenCom.createLevelLayer();
    },

    /**
     * 开始游戏
     */
    gameStart(){
        //玩家进入关卡扣除2个体力值
        var coin = parseInt(window.gameConfig.gameData.coin) - 2;
        window.gameConfig.saveGameData("coin", coin);
        this.labelTiLi.string = coin;

        window.gameLevelNum = this._curLevel;
        cc.director.loadScene("gameMain");
    },

    /**
     * 打开排行榜
     */
    openRankLayer(){

    }
});